package com.yisu.auction.manager.website.controller;


import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.OfferPriceLogArchive;
import com.yisu.auction.ewf.entity.PrizeArchive;
import com.yisu.auction.manager.core.service.IPrizeArchiveService;
import com.yisu.auction.manager.domain.request.loginPackage.LoginRequest;
import com.yisu.auction.manager.domain.util.Constants;
import com.yisu.auction.manager.domain.vo.employeePackageVos.EmployeeVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.yisu.auction.ewf.common.enums.ResponseCode.CUSTOMER_NOT_EXIST;
import static com.yisu.auction.ewf.common.enums.ResponseCode.PARAMETER_ERROR;
import static com.yisu.auction.ewf.common.enums.ResponseCode.SUCCESS;


/**
 * Created by LKL on 2016/9/12.
 */
@RequestMapping(value = "/user")
@Controller
public class UserController extends BaseController {

    @Resource
    private IPrizeArchiveService iPrizeArchiveService;

    @RequestMapping(value = "/archiveFile.json")
    @ResponseBody
    public JsonResult archiveFile(HttpServletRequest request, LoginRequest loginRequest) {
        try {
            final int sourceCount = iPrizeArchiveService.getArchiveFileCount();
            if (sourceCount > 10000) {
                ExecutorService executorService = Executors.newFixedThreadPool(1);
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        int forNum = sourceCount - 10000;
                        List<Long> prizeIds = iPrizeArchiveService.getTrueUserBuyPrizeId();
                        for (int i = 0; i < forNum; i++) {
                            iPrizeArchiveService.detailedArchive(prizeIds);
                        }
                        System.out.println("此批数据最终归档完成");
                    }
                };
                executorService.execute(runnable);
            }
            return new JsonResult(SUCCESS.code, "操作成功！");
        } catch (Exception e) {
            int i = 1;
        }
        return new JsonResult(SUCCESS.code, "操作成功！");
    }


    @RequestMapping(value = "/login.json")
    @ResponseBody
    public JsonResult login(HttpServletRequest request, LoginRequest loginRequest) {
        if (StringUtils.isEmpty(loginRequest.getEmployeeNumber())
                || StringUtils.isEmpty(loginRequest.getPassword())) {
            logger.error("Employee number and name can not empty!");
            return new JsonResult(PARAMETER_ERROR.code, PARAMETER_ERROR.msg);
        }
        if (!loginRequest.getPassword().equals("Ys1688")) {
            return new JsonResult(-1, "请输入正确的密码！");
        }
        EmployeeVo employee = new EmployeeVo();
        employee.setEmployeeNumber("0001");
        employee.setEmployeeName("超级管理员");
        if (employee != null) {
            logger.info("User[{}] logged in successfully.", loginRequest.getEmployeeNumber());
            request.getSession().setAttribute(Constants.USER_INFO_KEY, employee);
            return new JsonResult();
        } else {
            logger.error("User[{}] failed to login. cause: [{}].", loginRequest.getEmployeeNumber(), CUSTOMER_NOT_EXIST.msg);
            return new JsonResult(CUSTOMER_NOT_EXIST.code, CUSTOMER_NOT_EXIST.msg);
        }

    }

    @RequestMapping(value = "/logout.json")
    @ResponseBody
    public JsonResult logout(HttpServletRequest request) {
        EmployeeVo employee = (EmployeeVo) request.getSession().getAttribute(Constants.USER_INFO_KEY);
        request.getSession().removeAttribute(Constants.USER_INFO_KEY);

        logger.info("User[{}] logged out.", employee.getEmployeeName());
        return new JsonResult();
    }


    @RequestMapping(value = "/indexPage.htm")
    public String indexPage() {
        return "/main/index";
    }

    @RequestMapping(value = "/welcome.htm")
    public String welcome() {
        return "/main/welcome";
    }

}
